package com.guomao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;


@WebServlet("/LogoutServlet")
public class LogoutServlet extends HttpServlet {
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setCharacterEncoding("UTF-8");
        // 使会话失效
        HttpSession session = request.getSession(false);
        if (session != null) {
            session.invalidate();
        }
        
        // 删除记住我Cookie
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if ("rememberedUsername".equals(cookie.getName()) || 
                    "rememberedPassword".equals(cookie.getName())) {
                    cookie.setMaxAge(0); // 立即过期
                    response.addCookie(cookie);
                }
            }
        }
        
        // 重定向到登录页面
        response.sendRedirect("login.jsp");
    }
}